import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn, VxeTableGridOptions } from '#/adapter/vxe-table';
import type { SystemAuditLogApi } from '#/api';

import { $t } from '#/locales';

export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'username',
      label: $t('system.auditlog.username'),
    },
    {
      component: 'Input',
      fieldName: 'module',
      label: $t('system.auditlog.module'),
    },
    {
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [
          { label: 'GET', value: 'GET' },
          { label: 'POST', value: 'POST' },
          { label: 'PUT', value: 'PUT' },
          { label: 'DELETE', value: 'DELETE' },
          { label: 'PATCH', value: 'PATCH' },
        ],
      },
      fieldName: 'method',
      label: $t('system.auditlog.method'),
    },
    {
      component: 'Input',
      fieldName: 'summary',
      label: $t('system.auditlog.summary'),
    },
    {
      component: 'Input',
      fieldName: 'path',
      label: $t('system.auditlog.path'),
    },
    {
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [
          { label: '成功 (200)', value: 200 },
          { label: '创建成功 (201)', value: 201 },
          { label: '客户端错误 (400)', value: 400 },
          { label: '未授权 (401)', value: 401 },
          { label: '禁止访问 (403)', value: 403 },
          { label: '未找到 (404)', value: 404 },
          { label: '服务器错误 (500)', value: 500 },
        ],
      },
      fieldName: 'status',
      label: $t('system.auditlog.status'),
    },
    {
      component: 'RangePicker',
      fieldName: 'createTime',
      label: $t('system.auditlog.createTime'),
    },
  ];
}

export function useColumns<T = SystemAuditLogApi.AuditLog>(
  onActionClick?: OnActionClickFn<T>,
): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'id',
      title: $t('system.auditlog.id'),
      width: 80,
    },
    {
      field: 'username',
      title: $t('system.auditlog.username'),
      width: 120,
    },
    {
      field: 'module',
      title: $t('system.auditlog.module'),
      width: 120,
    },
    {
      field: 'method',
      title: $t('system.auditlog.method'),
      width: 100,
      cellRender: {
        name: 'CellTag',
        options: [
          { color: 'success', label: 'GET', value: 'GET' },
          { color: 'primary', label: 'POST', value: 'POST' },
          { color: 'warning', label: 'PUT', value: 'PUT' },
          { color: 'error', label: 'DELETE', value: 'DELETE' },
          { color: 'default', label: 'PATCH', value: 'PATCH' },
        ],
      },
    },
    {
      field: 'summary',
      title: $t('system.auditlog.summary'),
      minWidth: 150,
    },
    {
      field: 'path',
      title: $t('system.auditlog.path'),
      minWidth: 200,
    },
    {
      field: 'status',
      title: $t('system.auditlog.status'),
      width: 100,
      cellRender: {
        name: 'CellTag',
        options: [
          { color: 'success', label: '200', value: 200 },
          { color: 'success', label: '201', value: 201 },
          { color: 'warning', label: '400', value: 400 },
          { color: 'warning', label: '401', value: 401 },
          { color: 'warning', label: '403', value: 403 },
          { color: 'warning', label: '404', value: 404 },
          { color: 'error', label: '500', value: 500 },
        ],
      },
    },
    {
      field: 'response_time',
      title: $t('system.auditlog.responseTime'),
      width: 120,
      formatter: ({ cellValue }) => {
        if (cellValue < 100) return `${cellValue}ms`;
        if (cellValue < 1000) return `${cellValue}ms`;
        return `${(cellValue / 1000).toFixed(2)}s`;
      },
    },
    {
      field: 'created_at',
      title: $t('system.auditlog.createTime'),
      width: 180,
      formatter: 'formatDateTime',
    },
    {
      align: 'center',
      cellRender: {
        attrs: {
          nameField: 'username',
          nameTitle: $t('system.auditlog.username'),
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          {
            code: 'view',
            text: $t('common.view'),
          },
        ],
      },
      field: 'operation',
      fixed: 'right',
      title: $t('common.operation'),
      width: 100,
    },
  ];
}
